<?php
namespace Home\Controller;
use Think\Controller;
class CustomerController extends BackController {
	//未报名
	public function CustomerList(){
		$user = M('user')->select();
		$this->assign('sc_user',$user);
		
		$akey = array(1=>'非常重点',2=>'重点',3=>'一般',4=>'未知');
		$all_am = $this->all_am();
		$this->assign('all_am',$all_am);
		$this->assign('akey',$akey);
		
		$state_data = M('other')->where('pid=2')->field(true)->select();
		$this->assign('state_data',$state_data);
		$sc_student = M('sc_student');
		$where="";
		//校长
		if((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']==7)){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}elseif((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']!=7)){
			//销售
			$where['xsid'] = array('eq',session('qe_user')['id'],'and');
		}
		$where['state_id']  = array('not in',array('9','13'),'and');
		//电话搜索
		$phone = I('post.phone','','addslashes');
		if(!empty($phone)){
			$where['phone'] = array('like','%'.$phone.'%','and');
			$this->assign('phone',$phone);
		}
		//名称搜索
		$name = I('post.name','','addslashes');
		if(!empty($name)){
			$where['name'] = array('like','%'.$name.'%','and');
			$this->assign('name',$name);
		}
		//回访时间搜索
		$next_time = I('post.next_time','','addslashes');
		if(!empty($next_time)){
			$where['record_time'] = array('eq',$next_time,'and');
			$this->assign('next_time',$next_time);
		}
		//来源
		$am_id = I('post.am_id','','int');
		if(!empty($am_id)){
			$where['am_id'] = array('eq',$am_id,'and');
			$this->assign('am_id',$am_id);
		}
		//重点
		$akey_id = I('post.akey_id','','int');
		if(!empty($akey_id)){
			$where['akey_id'] = array('eq',$akey_id,'and');
			$this->assign('akey_id',$akey_id);
		}
		//状态
		$state_id = I('post.state_id','','int');
		if(!empty($state_id)){
			$where['state_id'] = array('eq',$state_id,'and');
			$this->assign('state_id',$state_id);
		}
		//就读学校
		$school = I('post.school','','addslashes');
		if(!empty($school)){
			$where['school'] = array('like','%'.$school.'%','and');
			$this->assign('school',$school);
		}
		//出生年月
		$birth_date = I('post.birth_date','','addslashes');
		if(!empty($birth_date)){
			$where['birth_date'] = array('eq',$birth_date,'and');
			$this->assign('birth_date',$birth_date);
		}
		$where['dt_id'] = array('eq',0,'and');
		//回访内容搜索
		$content = I('post.content','','addslashes');
		$count      = $sc_student->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$sc_data = $sc_student->where($where)->order('edit_time desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		if(!empty($content)){ 
			foreach($sc_data as $k=>$v){
				$sc_data[$k]['record_data'] = $this->record_data($v['id'],$content);
				$sc_data[$k]['xs_name'] = $this->username($v['xsid']);
				$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
				$sc_data[$k]['state_name'] = $this->basname($v['state_id']);
			}
			foreach($sc_data as $key=>$val){
				if(!empty($val['record_data'])){
					$ss[$key] = $val;
				}
			}
			$sc_data = $ss;
		}else{
			foreach($sc_data as $k=>$v){
				$sc_data[$k]['record_data'] = $this->record_data($v['id']);
				$sc_data[$k]['xs_name'] = $this->username($v['xsid']);
				$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
				$sc_data[$k]['state_name'] = $this->basname($v['state_id']);
			}
		}	
		//var_dump($sc_data);
		$this->assign('sc_data',$sc_data);
		$this->assign('page',$show);
		$this->assign('count',$count);
		$campus = M('other')->select();
		$this->assign('campus',$campus);
		$this->display();
	}
	//已报名
	public function CustomerList2(){
		$user = M('user')->select();
		$this->assign('sc_user',$user);
		
		$sc_student = M('sc_student');
		$where="";
		//校长
		if((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']==7)){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}elseif((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']!=7)){
			//销售
			$where['xsid'] = array('eq',session('qe_user')['id'],'and');
		}
		$where['state_id']  = array('in',array('9','13'),'and');
		$date = I('get.date','','addslashes');
		if($date != ''){
			$where['news_time'] = array('eq',$date,'and');
			$this->assign('date',$date);
		}
		$count      = $sc_student->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$sc_data = $sc_student->where($where)->order('edit_time desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($sc_data as $k=>$v){
			$sc_data[$k]['record_data'] = $this->record_data($v['id']);
			$sc_data[$k]['xs_name'] = $this->username($v['xsid']);
			$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
			$sc_data[$k]['state_name'] = $this->basname($v['state_id']);
		}
		$this->assign('sc_data',$sc_data);
		$this->assign('page',$show);
		$this->assign('count',$count);
		$campus = M('other')->select();
		$this->assign('campus',$campus);
		$this->display();
	}
	/**
	* 新增客户
	*/
	public function CustomerInput() {
			if(IS_POST) {
					//通过D实例化模型类
					$_POST['student_id'] = date("Ymd").rand(000000000,999999999);
					$_POST['news_time'] = date("Y-m-d");
					$phone = I('post.phone','','addslashes');
					$student = M('sc_student');
					$where['xsid'] = array('eq',I('post.xsid','','int'),'and');
					$where['state_id']  = array('not in',array('9','13'),'and');
					$all_count = $this->all_count(2);
					$count = $student->where($where)->count();
					if($count<$all_count){
						$student_show = $student->where('phone='.$phone)->find();
						$data['xsid'] = I('post.xsid','','int');
						$data['dt_id'] = 0; 
						$data['edit_time'] = date("Y-m-d H:i:s");
						if($student_show){
							if($student_show['up']==1){
								//已报名
								if($this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'该学生已报名')){
									$this->error('该学生已报名',U('Customer/studenterrors'));
								}
							}else{
								//未报名
								if(($student_show['dt_id']==1) and ($student_show['xid']==session('qe_user')['xid'])){
									//在本校区资源池
									if($student->where('phone='.$phone)->save($data) !== false) {
											$this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'资源池抽取');
											$this->success('资源池抽取客户成功',U('Customer/CustomerList'));
									}else {
											$this->error('资源池抽取客户失败',U('Customer/CustomerList'));
									}
								}elseif(($student_show['dt_id']==0) and ($student_show['xid']==session('qe_user')['xid'])){
									//不在本校区资源池,检查最新沟通记录日期
									$record = M('record')->where('sid='.$student_show['id'])->order('start_time desc')->find();
									$start_time = date("Y-m-d", strtotime("+1 months", strtotime($record['start_time'])));
									$news_time = date("Y-m-d");
									if($start_time<$news_time){
										//超出1个月
										if($student->where('phone='.$phone)->save($data) !== false) {
												$this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'销售名单');
												$this->success('销售名单抽取成功',U('Customer/CustomerList'));
										}else {
												$this->error('销售名单失败',U('Customer/CustomerList'));
										}
									}else{
										//沟通日期未超出一个月
										if($this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'在沟通时间以内')){
											$this->error('在沟通时间以内，无法抽取',U('Customer/studenterrors'));
										}
									}
								}elseif(($student_show['dt_id']==1) and ($student_show['xid']!=session('qe_user')['xid'])){
									//在跨校区资源池
									if($this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'跨校区资源池')){
										$this->error('跨校区资源池内，无法抽取',U('Customer/studenterrors'));
									}
								}elseif(($student_show['dt_id']==0) and ($student_show['xid']!=session('qe_user')['xid'])){
									//不在跨校区资源池
									if($this->errors($student_show['id'],$student_show['xid'],$student_show['xsid'],'跨校区销售名单')){
										$this->error('跨校区客户名单下，无法抽取',U('Customer/studenterrors'));
									}
								}
							}
						}else{
							if($student->create()){
								if($student->add($_POST)) {
										$this->success('新增成功',U('Customer/CustomerList'));
								}else {
										$this->error('新增失败',U('Customer/CustomerList'));
								}
							}
						}
					}else{
						$this->error('跟踪客户已达上限!',U('Customer/CustomerList'));
					}
			}else {
					$campus = M('other')->select();
					$this->assign('campus',$campus);
					$this->display();
			}
	}
	/**
	* 编辑客户
	*/
	public function Customeredit() {
			if(IS_POST) {
				$student = D('Student');
				$id = I('post.id','','int');
				$_POST['edit_time'] = date("Y-m-d H:i:s");
					if($student->create()) {
							if($student->where('id='.$id)->save($_POST) !== false) {
									$this->success('修改成功',U('Customer/CustomerList'));
							}else {
									$this->error('修改失败',U('Customer/CustomerList'));
							}
					}else {
							echo 0;exit;
					}
			}
	}
	public function detailedInformation(){
		$id = I('get.id','','int');
		$sc_student = M('sc_student')->where('id='.$id)->find();
		$sc_student['xs_name'] = $this->username($sc_student['xsid']);
		$sc_student['am_name'] = $this->basname($sc_student['am_id']);
		$sc_student['state_name'] = $this->basname($sc_student['state_id']);
		$sc_student['classname'] = $this->classname($sc_student['auditions_id']);
		$this->assign('sc_student',$sc_student);
		//沟通记录
		$record_data = $this->record_list($id);
		$this->assign('record_data',$record_data);
		
		$this->display();
	}
	public function auditions(){
		if(IS_POST){
			$id = I('post.id');
			$student = M('sc_student')->where('id='.$id)->find();
			if($student['auditions_id']){
				$data['class'] = M('gradesclass')->where('id='.$student['auditions_id'])->find();
				$data['tate'] = 1;
			}else{
				$where['class_state'] = array('eq',1,'and');
				$where['auditions_id'] = array('eq',1,'and');
				$where['xid'] = array('eq',session('qe_user')['xid'],'and');
				$where['end_time'] = array('gt',date('Y-m-d'),'and');
				$data['class'] = M('gradesclass')->where($where)->select();
				$data['tate'] = 0;
			}
			$this->ajaxReturn($data);
		}
	}
	public function look_img(){
		if(IS_POST){
			$id = I('post.id','','int');
			$sc_student = M('sc_student')->where('id='.$id)->find();
			$sc_student['xs_name'] = $this->username($sc_student['xsid']);
			$sc_student['am_name'] = $this->basname($sc_student['am_id']);
			$sc_student['x_name'] = $this->basname($sc_student['xid']);
			$sc_student['sc_name'] = $this->username($sc_student['scid']);
			$sc_student['state_name'] = M('other')->where('pid=2 and id!=9')->select();
			$sc_student['curriculum'] = M('curriculum')->where('xid='.session('qe_user')['xid'])->select();
			
			$sc_student['curriculum_name'] = $this->curname($sc_student['curriculum_id']);
			$sc_student['discount_name'] = $this->disname($sc_student['discount']);
			$this->ajaxReturn($sc_student);
		}
	}
	public function kecheng(){
		if(IS_POST){
			$curriculum = M('curriculum')->where('xid='.session('qe_user')['xid'])->select();
			$this->ajaxReturn($curriculum);
		}
	}
	public function youhui(){
		if(IS_POST){
			$curriculum_id = I('post.curriculum_id','','int');
			$activity = M('activity')->where('status=1 and bid='.$curriculum_id)->select();
			$this->ajaxReturn($activity);
		}
	}
	public function xuefei(){
		if(IS_POST){
			$discount = I('post.discount','','int');
			$activity = M('activity')->where('id='.$discount)->find();
			$curriculum = M('curriculum')->where('id='.$activity['bid'])->find();
			$cost = $curriculum['money']-$activity['amount'];
			$this->ajaxReturn($cost);
		}
	}
	public function record(){
		if(IS_POST) {
			//通过D实例化模型类
			$_POST['start_time'] = date("Y-m-d");
			$_POST['xsid'] = session('qe_user')['id'];
			$sid = I('post.sid');
			$data['edit_time'] =  date("Y-m-d H:i:s");
			$data['record_time'] = I('post.next_time');
			$data['auditions_id'] = I('post.auditions_id');
			if($data['auditions_id']){
				$cout = M('gradesclass')->where('id='.$data['auditions_id'])->find();
				$cout2 = M('sc_student')->where('auditions_id='.$data['auditions_id'])->count();
				if($cout2<$cout['enrollment_number']){
					M('sc_student')->where('id='.$sid)->save($data);
				}else{
					$this->error('试听人数已满',U('Customer/detailedInformation?id='.$_POST['sid']));
				}
			}
			$record = M('record');
			if($record->create()) {
					if($record->add($_POST)) {
						M('sc_student')->where('id='.$sid)->save($data);
							$this->success('新增成功',U('Customer/detailedInformation?id='.$_POST['sid']));
					}else {
							$this->error('新增失败',U('Customer/detailedInformation?id='.$_POST['sid']));
					}
			}else {
					$this->error($record->getError());
			}
		}
	}
	public function look_show(){
		if(IS_POST){
			$id = I('post.id','','int');
			$record_data = M('record')->where('id='.$id)->find();
			$record_data['xs_name'] = $this->username($record_data['xsid']);
			$student = M('sc_student')->where('id='.$record_data['sid'])->find();
			$record_data['classname'] = $this->classname($student['auditions_id']);
			$this->ajaxReturn($record_data);
		}
	}
	//放入资源池
	public function student_dt(){
		if(IS_POST){
			$id = I('post.id','','int');
			$data['xsid'] = 0;
			$data['dt_id'] = 1;
			if(M('sc_student')->where('id='.$id)->save($data) !== false){
				echo 1;exit;
			}else{
				echo 0;exit;
			}
		}
	}
	//销售抽取资源池客户
	public function CustomerPool(){
		if(IS_POST){
			$id = I('post.id','','int');
			$data['xsid'] = session('qe_user')['id'];
			$data['dt_id'] = 0;
			if(M('sc_student')->where('id='.$id)->save($data) !== false){
				echo 1;exit;
			}else{
				echo 0;exit;
			}
		}else{
			$sc_student = M('sc_student');
			$where['xsid'] = array('eq',0,'and');
			$where['dt_id'] = array('eq',1,'and');
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
			$count      = $sc_student->where($where)->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$sc_data = $sc_student->where($where)->order('edit_time desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			foreach($sc_data as $k=>$v){
				$sc_data[$k]['record_data'] = $this->record_data($v['id']);
				$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
				$sc_data[$k]['state_name'] = $this->basname($v['state_id']);
			}
			$this->assign('sc_data',$sc_data);
			$this->assign('count',$count);
			$this->assign('page',$page);
			
			$campus = M('other')->select();
			$this->assign('campus',$campus);
			$this->display();
		}
	}
	//客户冲突信息
	public function studenterrors(){
		$errors = M('errors');
		if((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']==7)){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}else{
			$where['xsid'] = array('eq',session('qe_user')['id'],'and');
		}
		//$where['old_xsid'] = array('eq',session('qe_user')['id'],'or');
		$errors_data = $errors->where($where)->order('id desc')->select();
		foreach($errors_data as $k=>$v){
			$errors_data[$k]['xs_name'] = $this->username($v['xsid']);
			$errors_data[$k]['x_name'] = $this->basname($v['xid']);
			$errors_data[$k]['oldxs_name'] = $this->username($v['old_xsid']);
			$errors_data[$k]['student_show'] = M('sc_student')->where('id='.$v['student'])->find();
			$errors_data[$k]['record'] = M('record')->where('sid='.$v['student'])->order('start_time desc')->find();
		}
		$this->assign('errors_data',$errors_data);
		//var_dump($errors_data);
		$this->display();
	}
	public function Classroom(){
		$class = M('gradesclass');
		$where="";
		if(session('qe_user')['xid']!=0){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}
		$where['class_state'] = array('eq',1,'and');
		$where['auditions_id'] = array('eq',1,'and');
		$count      = $class->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$class_list = $class->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($class_list as $k=>$v){
			$number = 0;
			//课程分类
			$class_list[$k]['lecturename'] = $this->curname($v['lecture']);
			//主讲老师
			$class_list[$k]['teachername'] = $this->teachername($v['teacher_id']);
			//助教
			$class_list[$k]['headmastername'] = $this->teachername($v['headmaster_id']);
			//校区
			$class_list[$k]['xname'] = $this->basname($v['xid']);
			//班级已排人数
			$student = M('sc_student')->where('auditions_id='.$v['id'])->select();
			foreach($student as $val){
				$number = $number+1;
			}
			$class_list[$k]['number'] = $number;
			$class_list[$k]['percentage'] = ceil($class_list[$k]['number']/$class_list[$k]['enrollment_number']*100);
		}
		$this->assign('class_list',$class_list);
		$this->display();
	}
	public function Classroomshow(){
		$id = I('get.id','','int');
		$class_list = M('gradesclass')->where('id='.$id)->find();
		//课程分类
		$class_list['lecturename'] = $this->curname($class_list['lecture']);
		//主讲老师
		$class_list['teachername'] = $this->teachername($class_list['teacher_id']);
		//助教
		$class_list['headmastername'] = $this->teachername($class_list['headmaster_id']);
		//校区
		$class_list['xname'] = $this->basname($class_list['xid']);
		
		//课程相关未排班学生
		$student = M('sc_student')->where('auditions_id='.$class_list['id'])->select();
		foreach($student as $k=>$v){
			$student[$k]['xs_name'] = $this->username($v['xsid']);
		}
		$this->assign('student',$student);
		//课程相关已排班学生
		$student_show = M('sc_student')->where('auditions_id='.$class_list['id'])->select();
		$number = 0;
		foreach($student_show as $k=>$v){
			$student_show[$k]['xs_name'] = $this->username($v['xsid']);
			$number += 1;
		}
		$class_list['number'] = $number;
		$this->assign('student_show',$student_show);
		$this->assign('class_list',$class_list);
		//var_dump($student);
		$this->display();
	}
}